package com.heer.gymnasium.module.team.views;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.my.views.MyLabelActivity;
import com.heer.gymnasium.module.team.beans.TeamBean;
import com.heer.gymnasium.module.team.beans.TeamDetailBean;
import com.heer.gymnasium.module.team.beans.TeamMemberResponseBean;
import com.heer.gymnasium.util.DialogUtils;
import com.zhihuiyun.library.commonadapter.QuickInterface;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.dialog.CommonDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 成员管理
 * Created by zy on 2018/3/6.
 */

public class MemberManageActivity extends BaseRefreshActivity {
    private final static int RES_ID_ROOT = R.layout.layout_member_manage;
    private final static int RES_ID_LIST = R.id.layout_member_manage_lv;

    private final static int RES_ID_ITEM_MEMBER = R.layout.item_team_member_manage;
    private final static int RES_ID_ITEM_MEMBER_ICON = R.id.item_team_member_manage_head_iv;
    private final static int RES_ID_ITEM_MEMBER_NAME = R.id.item_team_member_manage_name_tv;
    private final static int RES_ID_ITEM_MEMBER_COUNT = R.id.item_team_member_manage_count_tv;
    private final static int RES_ID_ITEM_MEMBER_INFO = R.id.item_team_member_manage_info_v;
    private final static int RES_ID_ITEM_MEMBER_REMOVE = R.id.item_team_member_manage_remove_v;
    private final static int RES_ID_ITEM_MEMBER_CHANGE = R.id.item_team_member_manage_change_v;

    @BindView(RES_ID_LIST)
    ListView listView;

    private QuickTypeAdapter adapter;
    private List<TeamDetailBean.Item> itemData;

    private String tid,captionId;
    private MemberManageActivity $this;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_ROOT);
        setRefreshResId(RES_ID_REFRESH_LAYOUT);
        itemData = new ArrayList<>();
        tid = getIntent().getStringExtra(EXTRA_ID);
        captionId = getIntent().getStringExtra(EXTRA_DATA);
    }

    @Override
    protected void initView() {
        super.initView();
        adapter = new QuickTypeAdapter(this,itemData, RES_ID_ITEM_MEMBER) {
            @Override
            public void bindData4View(ViewHolder holder, QuickInterface data, int type, int pos) {
                final TeamDetailBean.Item bean = (TeamDetailBean.Item) data;
                holder.setImg($this,RES_ID_ITEM_MEMBER_ICON,bean.getIconUrl());
                holder.setText(RES_ID_ITEM_MEMBER_NAME,bean.getName());
                holder.setText(RES_ID_ITEM_MEMBER_COUNT,bean.getActionCount());
                holder.setLis(RES_ID_ITEM_MEMBER_INFO,new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyLabelActivity.startActivity(context,bean.getId(),tid);
                    }
                });

                holder.setLis(RES_ID_ITEM_MEMBER_REMOVE,new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.showDialog($this, "", String.format("确认移除队员 %s?",bean.getName()), "", "", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DialogUtils.dismissDialog();
                                if (v.getId() == CommonDialog.RES_ID_POSITIVE){
                                    deleteMember(bean.getId());
                                }
                            }
                        });
                    }
                });

                holder.setLis(RES_ID_ITEM_MEMBER_CHANGE,new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DialogUtils.showDialog($this, "", String.format("确认转让队长给%s?",bean.getName()), "", "", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DialogUtils.dismissDialog();
                                if (v.getId() == CommonDialog.RES_ID_POSITIVE){
                                    changeCaptain(bean.getId());
                                }
                            }
                        });
                    }
                });
            }
        };
        listView.setAdapter(adapter);
    }

    private void changeCaptain(String id) {
        mAppHelper.apiService().changeCaptain(tid,id).enqueue(new BaseCallback<ApiResponse<Object>>() {
            @Override
            public void onSuccess(Call<ApiResponse<Object>> call, Response<ApiResponse<Object>> response) {
                ToastUtil.show($this,response.body().getMsg());
            }
        });
    }

    private void deleteMember(String id){
        mAppHelper.apiService().deleteMember(tid,id).enqueue(new BaseCallback<ApiResponse<Object>>() {
            @Override
            public void onSuccess(Call<ApiResponse<Object>> call, Response<ApiResponse<Object>> response) {
                loadData(false);
                ToastUtil.show($this,response.body().getMsg());
            }
        });
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        getCorpsMemberList(loadMore);
    }

    private void getCorpsMemberList(final boolean loadMore){
        mAppHelper.apiService().getCorpsMemberList(tid,String.valueOf(pageNo)).enqueue(new BaseCallback<ApiResponse<TeamMemberResponseBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<TeamMemberResponseBean>> call, Response<ApiResponse<TeamMemberResponseBean>> response) {
                if (!loadMore){
                    itemData.clear();
                }
                itemData.addAll(TeamDetailBean.getMemberList(response.body().getData().getData()));
                adapter.notifyDataSetChanged();
                layoutRefresh.setRefreshing(false);
            }

            @Override
            public void onError(Call<ApiResponse<TeamMemberResponseBean>> call, Throwable t) {
                super.onError(call, t);
                layoutRefresh.setRefreshing(false);
            }

            @Override
            public void onTokenTimeOut() {
                layoutRefresh.setRefreshing(false);
                super.onTokenTimeOut();
            }
        });
    }

    public static void startActivity(Activity context, String tid,String captionId,int requestCode){
        Intent mIntent = new Intent(context,MemberManageActivity.class);
        mIntent.putExtra(EXTRA_ID,tid);
        mIntent.putExtra(EXTRA_DATA,captionId);
        context.startActivityForResult(mIntent,requestCode);
    }

}
